// pages/zhuce/zhuce.js
const urlApi = require('../../utils/server.api.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex: ["男", "女"],
    sexIndex: 3,
    date: "",
    verfiyCode: 0,
    type: 0,
    registerOrUpdate: "",
    title: "",
    phone: "",
    disabled: "",
    username: "",
    id: "",
    addr: "",
    url: "",
    hidden: true,
    btnValue: "",
    btnDisabled:false,
    code: "",
    second: 60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.type)
    if (options.type == 0) {
      wx.setNavigationBarTitle({
        title: "注册",
      })
      this.setData({
        url: urlApi.patientRegister(),
        registerOrUpdate: "注册"
      })
    } else if (options.type == 1) {
      wx.setNavigationBarTitle({
        title: "信息修改",
      })
      this.setData({
        disabled: "disabled",
        username: app.globalData.userInfo.name,
        phone: app.globalData.userInfo.phone,
        id: app.globalData.userInfo.idCard,
        sexIndex: app.globalData.userInfo.sex,
        date: app.globalData.userInfo.birthday,
        addr: app.globalData.userInfo.address,
        url: urlApi.updatePatientInfo(),
        registerOrUpdate: "修改"
      })
    }
    this.setData({
      type: options.type,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  create_zhuce: function (e) {
    console.log(e.detail.value)
    if (this.data.date == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "出生日期不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["username"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "姓名不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["phone"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "手机号不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (!/^1[3456789]\d{9}$/.test(e.detail.value["phone"])) {
      this.data.verfiyCode++;
      wx.showToast({
        title: "手机号格式错误",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["id"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "身份证不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["addr"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "住址不能为空",
        icon: 'none',
        duration: 2000
      })
    } 
    if (this.data.verfiyCode == 0 && this.data.type == 0) {
      if (this.data.code == "") {
        wx.showToast({
          title: "验证码不能为空",
          icon: 'none',
          duration: 2000
        })
      } else {
        wx.request({
          url: this.data.url + "?username=" + e.detail.value["username"] + "&phone=" + e.detail.value["phone"] + "&id=" + e.detail.value["id"] + "&sex=" + this.data.sexIndex + "&date=" + this.data.date + "&addr=" + e.detail.value["addr"] + "&code=" + this.data.code,
          method: "post",
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res.data);
            if (res.data.code == 200) {
              wx.showToast({
                title: "注册成功",
                duration: 2000
              })
              wx.switchTab({
                url: '../login/login',
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 2
                })
              }, 2000)
            } else {
              wx.showToast({
                title: "注册失败，请稍后再试",
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    } else if (this.data.verfiyCode == 0 && this.data.type == 1) {
      wx.request({
        url: this.data.url + "?username=" + e.detail.value["username"] + "&phone=" + e.detail.value["phone"] + "&id=" + e.detail.value["id"] + "&sex=" + this.data.sexIndex + "&date=" + this.data.date + "&addr=" + e.detail.value["addr"],
        method: "put",
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == 200) {
            app.globalData.userInfo = null;
            app.globalData.userInfo = res.data.data
            wx.showToast({
              title: "修改成功，请重新登录",
              duration: 2000
            })
            wx.navigateTo({
              url: '../personal/personal',
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 2
              })
            }, 2000)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },

  //手机号输入
  bindPhoneInput(e) {
    var val = e.detail.value;
    this.setData({
      phone: val
    })
    if(val != ''){
      this.setData({
        hidden: false,
        btnValue: '获取验证码'
      })
    }else{
      this.setData({
        hidden: true
      })
    }
  },
  //验证码输入
  bindCodeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },

  //获取短信验证码
  getCode(e) {
    console.log('获取验证码');
    var that = this;
    wx.request({
      url: urlApi.sendSms() + "/" + that.data.username,
      method: "post",
      header: {
        'content-type': 'application/json'
      },
      success: function (response) {
        if (response.data.code == 200) {
          that.timer();
          wx.showModal({
            title: '提示',
            content: '验证码发送成功',
            complete: (res) => {
              if (res.cancel) {
                
              }
          
              if (res.confirm) {
                
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: response.data.msg,
            complete: (res) => {
              if (res.cancel) {
                
              }
          
              if (res.confirm) {
                
              }
            }
          })
        }
      }
    })
  },
    
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          var second = this.data.second - 1;
          this.setData({
            second: second,
            btnValue: second+'秒',
            btnDisabled: true
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              btnValue: '获取验证码',
              btnDisabled: false
            })
            resolve(setTimer)
          }
        }
        , 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },
  
  bindChange: function(options) {
    this.setData ({
      date: options.detail.value
    })
  },

  bindSexChange: function(e) {
    this.setData ({
      sexIndex: e.detail.value
    })
  }
})